package cn.topevery.garden.dal.logic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjFilesDao;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.query.ProjFilesQuery;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.entity.KeyValue;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目附件表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjFilesLogic extends BaseLogic<ProjFiles> {

    @Autowired
    ProjFilesDao projFilesDao;

    @Override
    public BaseEntityDao<ProjFiles, Integer> getDao() {
        return projFilesDao;
    }

    /**
     * 根据projId获得附件列表
     *
     * @param projId
     * @return
     */
    public List<ProjFilesVO> listByProjId(String projId) {
        ProjFilesQuery query = new ProjFilesQuery();
        query.setProjId(projId);
        return projFilesDao.list(query);
    }

    /**
     * 根据项目ID和各阶段的业务ID 查询附件列表
     * @param projId
     * @param projStageId
     * @return
     */
    public List<ProjFilesVO> listByProjIdAndStageId(String projId, String projStageId) {
        ProjFilesQuery query = new ProjFilesQuery();
        query.setProjId(projId);
        query.setProjStageId(projStageId);
        return projFilesDao.list(query);
    }


    /**
     * 根据项目ID和各阶段的业务编码 查询附件列表
     * @param projId
     * @param stageTypeCode
     * @return
     */
    public List<ProjFilesVO> listByProjIdAndStageTypeCode(String projId, String stageTypeCode) {
        ProjFilesQuery query = new ProjFilesQuery();
        query.setProjId(projId);
        query.setProjStageTypeCode(stageTypeCode);
        return projFilesDao.list(query);
    }

    /**
     * 获取列表
     *
     * @param query
     */
    public List<ProjFilesVO> list(ProjFilesQuery query) {
        return projFilesDao.list(query);
    }

    /**
     * 获取分页列表
     *
     * @param query
     */
    public DataPager<ProjFilesVO> listPage(ProjFilesQuery query) {
        return projFilesDao.listPage(query);
    }


    public Boolean updateFilesByProjStageIds(List<String> projStageIds, String projId, String userId) {
        if (CollUtil.isEmpty(projStageIds)) {
            throw new RuntimeException("传入的项目阶段ID不能为空！");
        }
        return projFilesDao.deleteByProjIdAndProjStageIds(userId, projId, projStageIds);
    }

    /**
     * 校验附件并且设置阶段ID
     * @param files
     * @param stageId // 阶段ID就是各业务表的业务ID
     */
    public void checkFilesAndSetStageId(List<ProjFiles> files, String stageId) {
        if (CollUtil.isEmpty(files)) {
            return;
        }
        for(ProjFiles file : files) {
            if (ObjectUtil.isEmpty(file.getFileId())) {
                throw new RuntimeException("传入的附件ID不能为空！");
            }
            if (ObjectUtil.isEmpty(file.getProjStageTypeCode())) {
                throw new RuntimeException("传入的附件项目阶段编码不能为空！");
            }
            if (ObjectUtil.isNotEmpty(stageId)) {
                file.setProjStageId(stageId);
            }
        }
    }

    public boolean updateSqlUpdateMate(SqlUpdateMate updateMate) {
        return projFilesDao.updateSqlUpdateMate(updateMate);

    }

    public List<ProjFiles> getListEntityByColumns(List<KeyValue> list) {
        return projFilesDao.getListEntityByColumns(list);
    }
}
